package com.ktjiaoyu.service.impl;

import com.ktjiaoyu.entity.Supplier;
import com.ktjiaoyu.entity.SysUser;
import com.ktjiaoyu.mapper.SupplierMapper;
import com.ktjiaoyu.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("supplierServiceImpl")
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    private SupplierMapper supplierMapper;
    @Override
    public List<Supplier> getSupplierListByName(Supplier supplier) {
        return supplierMapper.getSupplierListByName(supplier);
    }
// 批量插入
    //Transactional 是一个事务注解，保证数据一致性
    @Transactional
    @Override
    public int insert(List<Supplier> supplierList) {
        int rows=0;
        for (int i = 0; i < supplierList.size(); i++) {
            //拿出每一条用户数据插入
            //抛出异常
            if (i==1){
                throw new RuntimeException("模拟数据库异常");
            }
            Supplier supplier=supplierList.get(i);
            rows+=supplierMapper.insert(supplier);
        }
        return rows;
    }

    @Override
    public int delete(Long id) {
        return supplierMapper.delete(id);
    }
}
